#include <iostream>
#include <memory>

using std::cout;
using std::endl;
using std::weak_ptr;
using std::shared_ptr;

void test()
{
    weak_ptr<int> wp;

    //块作用域
    {
        shared_ptr<int> sp(new int(10));
        wp = sp;//赋值
        /* weak_ptr<int> wp(sp); */
        cout << "sp.use_count() = " << sp.use_count() << endl;
        cout << "wp.use_count() = " << wp.use_count() << endl;

        shared_ptr<int> sp2 = wp.lock();
        if(sp2)
        {
            cout << "提升成功";
            cout << *sp2 << endl;
        }
        else
        {
            cout << "提升失败，托管的空间已经销毁" << endl;
        }

        cout << endl;
        bool flag = wp.expired();//use_count() == 0
        if(flag)
        {
            cout << "托管的空间已经销毁了" << endl;
        }
        else
        {
            cout << "托管的空间还在" << endl;
        }
    }

    cout << endl;
    shared_ptr<int> sp2 = wp.lock();
    if(sp2)
    {
        cout << "提升成功";
        cout << *sp2 << endl;
    }
    else
    {
        cout << "提升失败，托管的空间已经销毁" << endl;
    }
        
    cout << endl;
    bool flag = wp.expired();
    if(flag)
    {
        cout << "托管的空间已经销毁了" << endl;
    }
    else
    {
        cout << "托管的空间还在" << endl;
    }
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

